/*************************************************************
 * @file remote.c
 * @author MicroOrange
 * @brief 遥控器数据读取
 * @date 2022.4.14
 *************************************************************/
#include "tim.h"
#include "remote.h"


t_channel_feature channel[9];

void _Remote_ChannelConvert(t_channel_feature *channel){
    if (channel->type == CHANNEL_TYPE_CONTINUOUS){
        double deg = (double )(channel->source_value-channel->source_min)/(channel->source_max-channel->source_min);
        channel->target_value = channel->target_min + deg*(channel->target_max - channel->target_min);
    } else if (channel->type == CHANNEL_TYPE_DISCRETE_2){
        if (channel->source_value<1300)
            channel->target_value = 0;
        else
            channel->target_value = 1;
    } else if (channel->type == CHANNEL_TYPE_DISCRETE_3)
        if (channel->source_value<1200)
            channel->target_value = -1;
        else if (channel->source_value>1400)
            channel->target_value = 1;
        else
            channel->target_value = 0;

}

void Remote_ChannelInit(t_channel_feature *channel, int type, int s_min, int s_max, int t_min, int t_max){
    channel->type = type;
    channel->source_min = s_min;
    channel->source_max = s_max;
    channel->target_min = t_min;
    channel->target_max = t_max;
}

void Remote_SetChannelValue(int channel_index, int value){
    channel[channel_index].source_value = value;
}

int Remote_GetChannelValue(int channel_index){
    _Remote_ChannelConvert(&channel[channel_index]);
    return channel[channel_index].target_value;
}

void Remote_Init(){
    HAL_TIM_Base_Start(&htim4);
    HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_3);
    HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_4);
    HAL_TIM_Base_Start(&htim3);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_3);
    HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_4);

    Remote_ChannelInit(&channel[0], CHANNEL_TYPE_CONTINUOUS, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[1], CHANNEL_TYPE_CONTINUOUS, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[2], CHANNEL_TYPE_CONTINUOUS, 904, 1350, 0, 100);
    Remote_ChannelInit(&channel[3], CHANNEL_TYPE_CONTINUOUS, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[4], CHANNEL_TYPE_DISCRETE_3, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[5], CHANNEL_TYPE_CONTINUOUS, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[6], CHANNEL_TYPE_DISCRETE_2, 904, 1796, -100, 100);
    Remote_ChannelInit(&channel[7], CHANNEL_TYPE_DISCRETE_2, 904, 1796, -100, 100);
}


